home *** CD-ROM | disk | FTP | other *** search
/ Almathera Ten Pack 3: CDPD 3 / Almathera Ten on Ten - Disc 3: CDPD3.iso / scope / 001-025 / scopedisk11 / filemap1 / fm.c < prev    next >
C/C++ Source or Header  |  1995-03-18  |  8KB  |  370 lines

  1. /*  :ts=8 bk=0
  2.  * File mapper.  Uses trackdisk.device to grab sectors and traverse the
  3.  * filesystem the hard way to find out what sectors a particular file
  4.  * occupies.
  5.  *
  6.  * Crufted together by Leo Schwab while waiting for an open line on the WELL.
  7.  *    8608.19
  8.  * Finally finished:    8609.16
  9.  * I could have done it quicker if I hadn't started working for Wendy's.
  10.  *
  11.  * Note: This program is only valid for 3.5" floppy drives.
  12.  */
  13.  
  14. #include <exec/types.h>
  15. #include <exec/memory.h>
  16. #include <intuition/intuition.h>
  17. #include <libraries/dos.h>
  18. #include <devices/trackdisk.h>
  19. #include "things.h"
  20.  
  21.  
  22. extern struct NewWindow    windef;
  23. extern struct IntuiText    errmsg, ok;
  24. extern struct Gadget    gad[];
  25. extern char        filename[], devname[];
  26.  
  27. struct Window    *win;
  28. struct RastPort *rp;
  29. struct InfoData *id;
  30. struct IOExtTD    *diskreq;
  31. struct MsgPort    *diskport;
  32. ULONG        diskchangecount, *diskbuffer, bitmap[SIZE];
  33. int        bmsect;
  34. void        *IntuitionBase, *GfxBase, *lok;
  35.  
  36.  
  37. main (ac, av)
  38. char *av[];
  39. {
  40.     struct IntuiMessage *msg;
  41.     struct Gadget *ptr;
  42.     int class;
  43.  
  44.     if (ac) {
  45.         ac--;  av++;
  46.         if (ac) {
  47.             strcpy (devname, *av);
  48.             ac--;  av++;
  49.         } else
  50.             strcpy (devname, "df0:");
  51.  
  52.         if (ac)
  53.             strcpy (filename, *av);
  54.         else
  55.             strcpy (filename, ":");
  56.     }
  57.     openstuff ();
  58.     drawgrid ();
  59.     setdev ();
  60.  
  61.     /*  Process IntuiEvents  */
  62.     for ever {
  63.         WaitPort (win -> UserPort);
  64.         msg = GetMsg (win -> UserPort);
  65.         class = msg -> Class;
  66.         ptr = (struct Gadget *) msg -> IAddress;
  67.         ReplyMsg (msg);
  68.         if (class == CLOSEWINDOW)
  69.             break;
  70.         else if (class == GADGETUP) {
  71.             if (ptr == gad) {        /*  New filename  */
  72.                 drawbitmap ();
  73.                 findfile (filename);
  74.             } else if (ptr == &gad[1])    /*  New device  */
  75.                 setdev ();
  76.             else if (ptr == &gad[2]) {    /*  Refresh screen  */
  77.                 getbitmap ();
  78.                 drawbitmap ();
  79.                 findfile (filename);
  80.             }
  81.         }
  82.     }
  83.     closestuff ();
  84. }
  85.  
  86.  
  87. setdev ()
  88. {
  89.     int unit;
  90.  
  91.     /*
  92.      * This rather lengthy series of DOS calls is needed to turn DOS
  93.      * device names into unit numbers that OpenDevice() can deal with.
  94.      */
  95.     if (!(lok = Lock (devname, ACCESS_READ)))
  96.         die ("Can't obtain lock for specified device.");
  97.  
  98.     if (!(id = AllocMem ((long) sizeof (*id), MEMF_CLEAR)))
  99.         die ("Can't get InfoData memory.");
  100.  
  101.     if (!Info (lok, id))
  102.         die ("Call to Info() failed.");
  103.  
  104.     if (id -> id_DiskType == ID_NO_DISK_PRESENT)
  105.         die ("No disk in drive.");
  106.  
  107.     unit = id -> id_UnitNumber;
  108.     FreeMem (id, (long) sizeof (*id));  id = NULL;
  109.     UnLock (lok);  lok = NULL;
  110.     opendisk (unit);
  111.     getbitmap ();
  112.     drawbitmap ();
  113. }
  114.  
  115. getbitmap ()
  116. {
  117.     register int i;
  118.  
  119.     MotorOn ();
  120.     GetSector ((long) ROOTBLOCK);
  121.     bmsect = diskbuffer [BITMAPINDEX];
  122.     GetSector ((long) bmsect);
  123.     MotorOff ();
  124.     for (i=0; i<SIZE; i++)
  125.         bitmap [i] = diskbuffer [i];
  126. }
  127.  
  128. drawbitmap ()
  129. {
  130.     register int i, n, l, p;
  131.     int free = NUMBLOCKS-2;
  132.     long k, x, y;
  133.     char buf[80];
  134.  
  135.     SetDrMd (rp, JAM1);
  136.     SetAPen (rp, 3L);
  137.  
  138.     /*  Show sectors 0 and 1 (always allocated)  */
  139.     RectFill (rp, XOFF+1, YOFF+1, XX+XOFF-1, YY+YY+YOFF-1);
  140.     l = 2;
  141.     for (i=1; i<=NUMLONGS; i++) {
  142.         k = bitmap[i];
  143.         for (n=0; n<32; n++) {
  144.             /*  Bits progress from low to high order  */
  145.             if (i<NUMLONGS || n<30) {    /*  Ignore last two  */
  146.                 /*  Perform icky conversion  */
  147.                 x = (l / 22) * XX + XOFF;
  148.                 y = (l % 22) * YY + YOFF;
  149.                 if (y >= BRKOVER)
  150.                     y += SEP;
  151.  
  152.                 /*
  153.                  * The following incantation basically means,
  154.                  * don't draw sectors that don't need to be
  155.                  * drawn.
  156.                  */
  157.                 p = ReadPixel (rp, x+1, y+1);
  158.                 if (~k & 1) {
  159.                     free--;
  160.                     if (p != 3) {
  161.                         SetAPen (rp, 3L);
  162.                         RectFill (rp, x+1, y+1,
  163.                               x+XX-1, y+YY-1);
  164.                     }
  165.                 } else if (p) {
  166.                     SetAPen (rp, 0L);
  167.                     RectFill
  168.                      (rp, x+1, y+1, x+XX-1, y+YY-1);
  169.                 }
  170.                 k >>= 1;
  171.             }
  172.             l++;    /*  Increment sector number  */
  173.         }
  174.     }
  175.  
  176.     /*  Do labels  */
  177.     SetAPen (rp, 1L);
  178.     SetBPen (rp, 0L);
  179.     SetDrMd (rp, JAM2);
  180.     sprintf (buf, "Bitmap on sector %-4d", bmsect);
  181.     Move (rp, XOFF, LABEL_Y);
  182.     Text (rp, buf, (long) strlen (buf));
  183.     sprintf (buf, "Sectors free: %-4d", free);
  184.     Move (rp, 250L, LABEL_Y);
  185.     Text (rp, buf, (long) strlen (buf));
  186.     sprintf (buf, "Allocated: %-4d", (int) NUMBLOCKS-free);
  187.     Move (rp, 450L, LABEL_Y);
  188.     Text (rp, buf, (long) strlen (buf));
  189.     Move (rp, NUMCYLS*XX+XOFF+10, NUMSECS*YY/2+YOFF+2);
  190.     Text (rp, "Surface 0", 9L);
  191.     Move (rp, NUMCYLS*XX+XOFF+10, NUMSECS*YY/2+BRKOVER+SEP+2);
  192.     Text (rp, "Surface 1", 9L);
  193. }
  194.  
  195. drawgrid ()    /*  Draw grid and labels so we can see  */
  196. {
  197.     long x, y;
  198.  
  199.     SetDrMd (rp, JAM1);
  200.     SetAPen (rp, 1L);
  201.     for (x=XOFF; x<=80*XX+XOFF; x += XX) {
  202.         Move (rp, x, YOFF);
  203.         Draw (rp, x, YOFF+11*YY);
  204.         Move (rp, x, BRKOVER+SEP);
  205.         Draw (rp, x, BRKOVER+SEP+11*YY);
  206.     }
  207.     for (y=0; y<=11*YY; y += YY) {
  208.         Move (rp, XOFF, y+YOFF);
  209.         Draw (rp, XOFF+80*XX, y+YOFF);
  210.         Move (rp, XOFF, y+SEP+BRKOVER);
  211.         Draw (rp, XOFF+80*XX, y+SEP+BRKOVER);
  212.     }
  213.  
  214.     for(x = 6; x < 80; x+= 5)    /* Djj */
  215.     {
  216.         Move (rp, XOFF+x*XX-XX/2, YOFF);
  217.         Draw (rp, XOFF+x*XX-XX/2, YOFF-2);
  218.     }
  219.  
  220. /* Djj */
  221.     Move (rp, XOFF+10*XX-4, YOFF-3);  Text (rp, "10", 2L);
  222.     Move (rp, XOFF+20*XX-4, YOFF-3);  Text (rp, "20", 2L);
  223.     Move (rp, XOFF+30*XX-4, YOFF-3);  Text (rp, "30", 2L);
  224.     Move (rp, XOFF+40*XX-4, YOFF-3);  Text (rp, "40", 2L);
  225.     Move (rp, XOFF+50*XX-4, YOFF-3);  Text (rp, "50", 2L);
  226.     Move (rp, XOFF+60*XX-4, YOFF-3);  Text (rp, "60", 2L);
  227.     Move (rp, XOFF+70*XX-4, YOFF-3);  Text (rp, "70", 2L);
  228.  
  229.     /*  Draw map markings  */
  230.     Move (rp, XOFF+XX/2, YOFF);      Draw (rp, XOFF+XX/2, YOFF-3);
  231.     Move (rp, XOFF+80*XX-XX/2, YOFF); Draw (rp, XOFF+80*XX-XX/2, YOFF-3);
  232.     Move (rp, XOFF, YOFF+YY/2);      Draw (rp, XOFF-3, YOFF+YY/2);
  233.     Move (rp, XOFF, YOFF+YY*10+YY/2); Draw (rp, XOFF-3, YOFF+YY*10+YY/2);
  234.     Move (rp, XOFF-1, YOFF-4);      Text (rp, "0", 1L);
  235.     Move (rp, XOFF+79*XX-1, YOFF-4);  Text (rp, "79", 2L);
  236.     Move (rp, XOFF-12, YOFF+6);      Text (rp, "0", 1L);
  237.     Move (rp, XOFF-20, YOFF+11*YY);   Text (rp, "10", 2L);
  238. }
  239.  
  240. marksector (n, color)
  241. long n;
  242. int color;
  243. {
  244.     register int x, y;
  245.  
  246.     x = (n / 22) * XX + XOFF;
  247.     y = (n % 22) * YY + YOFF;
  248.     if (y >= BRKOVER)
  249.         y += SEP;
  250.     SetAPen (rp, (long) color);
  251.     RectFill (rp, x+1L, y+1L, x+XX-1L, y+YY-1L);
  252. }
  253.  
  254. openstuff ()
  255. {
  256.     if (!(IntuitionBase = OpenLibrary ("intuition.library", REV))) {
  257.         /*
  258.          * If we can't open Intuition, then we can't use
  259.          * AutoRequest ()
  260.          */
  261.         printf ("Intuition failed; you'll have to use logic.\n");
  262.         closestuff ();
  263.         exit (100);
  264.     }
  265.  
  266.     if (!(GfxBase = OpenLibrary ("graphics.library", REV))) {
  267.         printf ("Art shop closed.\n");
  268.         closestuff ();
  269.         exit (100);
  270.     }
  271.  
  272.     if (!(win = OpenWindow (&windef))) {
  273.         printf ("Window painted shut.\n");
  274.         closestuff ();
  275.         exit (100);
  276.     }
  277.     rp = win -> RPort;
  278.  
  279.     if (!(diskport = CreatePort (NULL, NULL)))
  280.         die ("No port.");
  281.  
  282.     if (!(diskreq = CreateExtIO (diskport, (long) sizeof (*diskreq))))
  283.         die ("Can't make IO block.");
  284.  
  285.     if (!(diskbuffer = AllocMem (BLOCKSIZE, MEMF_CLEAR | MEMF_CHIP)))
  286.         die ("Can't allocate disk buffer.");
  287. }
  288.  
  289. opendisk (unit)
  290. int unit;
  291. {
  292.     long err;
  293.     char *buf[80];
  294.  
  295.     /*  We may be changing units, so close it if it's open  */
  296.     if (diskreq -> iotd_Req.io_Device) {
  297.         CloseDevice (diskreq);
  298.         diskreq -> iotd_Req.io_Device = NULL;
  299.     }
  300.  
  301.     if (err = OpenDevice (TD_NAME, (long) unit, diskreq, NULL)) {
  302.         sprintf (buf, "Can't get at disk; err = %ld.", err);
  303.         die (buf);
  304.     }
  305.  
  306.     diskreq -> iotd_Req.io_Command = TD_CHANGENUM;
  307.     DoIO (diskreq);
  308.     diskchangecount = diskreq -> iotd_Req.io_Actual;
  309. }
  310.  
  311. closestuff ()
  312. {
  313.     if (lok)
  314.         UnLock (lok);
  315.     if (diskreq) {
  316.         /*
  317.          * Apparently, if OpenDevice() fails, it fills in the
  318.          * io_Device field with -1.  This pretty much blows all
  319.          * my previous code out of the water, which assumed it got
  320.          * filled in with 0.  Sigh.  Why don't they tell us these
  321.          * things?
  322.          */
  323.         if ((long) diskreq -> iotd_Req.io_Device != -1L)
  324.             CloseDevice (diskreq);
  325.         DeleteExtIO (diskreq, (long) sizeof (*diskreq));
  326.     }
  327.  
  328.     if (diskbuffer)
  329.         FreeMem (diskbuffer, (long) BLOCKSIZE);
  330.     if (id)
  331.         FreeMem (id, (long) sizeof (*id));
  332.     if (diskport)
  333.         DeletePort (diskport);
  334.     if (win)
  335.         CloseWindow (win);
  336.     if (GfxBase)
  337.         CloseLibrary (GfxBase);
  338.     if (IntuitionBase)
  339.         CloseLibrary (IntuitionBase);
  340. }
  341.  
  342. notice (str)    /*  For non-fatal errors  */
  343. UBYTE *str;
  344. {
  345.     MotorOff ();
  346.     errmsg.IText = str;
  347.     AutoRequest (win, &errmsg, NULL, &ok, NULL, NULL,
  348.              TextLength (rp, str, (long) strlen (str)) + 40, 46L);
  349. }
  350.  
  351. die (str)    /*  For fatal errors  */
  352. UBYTE *str;
  353. {
  354.     errmsg.IText = str;
  355.     AutoRequest (win, &errmsg, NULL, &ok, NULL, NULL,
  356.              TextLength (rp, str, (long) strlen (str)) + 40, 46L);
  357.     closestuff ();
  358.     exit (100);
  359. }
  360.  
  361. /*    Guess....
  362. debug (str)
  363. char *str;
  364. {
  365.     printf (str);
  366.     getchar ();
  367.  
  368. }
  369. */
  370.